﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;
using WebBanThuoc.Server;
using System.Globalization;
using System.Text.RegularExpressions;

namespace WebBanThuoc
{
    public partial class Register : System.Web.UI.Page
    {
         IServerWeb sw = new ServerWeb();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)  loadData();
            this.LoadComplete += new EventHandler(Register_LoadComplete);
        }
        void Register_LoadComplete(object sender, EventArgs e)
        {
            sw.closeConnection();
        }
        private void loadData()
        {
                tfname.Text = null;
                tpword.Text = null;
                taddress.Text = null;
                temail.Text = null;
                tphone.Text = null;
                tpword.Enabled = true;
         }
       
        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            if (temail.Text.Trim().Length > 0)
            {
                ccJoin.ValidateCaptcha(TextBox1.Text);
                if (!ccJoin.UserValidated)
                {
                    Label1.Text = "Mã bảo vệ chưa chính xác";
                    return;
                }
                if (!Page.IsValid) return;
                             
                KhachHang kh = new KhachHang();
               // System.Text.RegularExpressions.Regex.Replace(tfname.Text, "'", string.Empty);
                kh.HoTen = StripHtml(tfname.Text,false);
                kh.Email = StripHtml(temail.Text, false);
                kh.DiaChi = StripHtml(taddress.Text,false);
                kh.SoDienThoai = StripHtml(tphone.Text,false);
                kh.NgaySinh = Function.Func.MinValue_SmallDatetime();
                kh.NgayTao = DateTime.Now;
                kh.Phai = "";
                kh.MatKhau = StripHtml(tpword.Text, false);
                //thêm khách hàng
                KhachHang_DAO daoKH = new KhachHang_DAO(sw.Connection);
                kh.IDKhachHang = daoKH.createIDKhachHang();
                int okthemKH = daoKH.add(kh);
                if (okthemKH==1)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "alert('Tạo tài khoản thành công!');", true);
                    Response.Redirect("Login.aspx");
                }

            }
        }
        public static string StripHtml(string html, bool allowHarmlessTags)
        {
            if (html == null || html == string.Empty)
                return string.Empty;

            if (allowHarmlessTags)
                return System.Text.RegularExpressions.Regex.Replace(html, "", string.Empty);

            return System.Text.RegularExpressions.Regex.Replace(html, "<[^>]*(>|$)'", string.Empty); 
        }
        protected void CancelPushButton_Click(object sender, EventArgs e)
        {
            loadData();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
           
        }
        protected void Email_CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = false;
            KhachHang_DAO daoKH = new KhachHang_DAO(sw.Connection);
            KhachHang k = daoKH.checkEmail(temail.Text.Trim());
            if (k == null) args.IsValid = true;

        }
    }
}